package com.framework.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.core.log.annotation.AutoLog;
import com.framework.core.result.R;
import com.framework.system.entity.dto.SysConfigAddDTO;
import com.framework.system.entity.dto.SysConfigEditDTO;
import com.framework.system.entity.dto.SysConfigQueryDTO;
import com.framework.system.entity.vo.SysConfigListVO;
import com.framework.system.entity.vo.SysConfigVO;
import com.framework.system.service.ISysConfigService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

/**
 * <p>
 * 系统配置 前端控制器
 * </p>
 *
 * @author Kevin
 * @since 2021-06-10
 */
@RestController
@RequestMapping("/system/sysConfig")
@AllArgsConstructor
public class SysConfigController {

    private ISysConfigService sysConfigService;

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    @SaCheckPermission("system:sysConfig:selectPage")
    @AutoLog(module = "参数管理", title = "分页查询")
    public R<Page<SysConfigListVO>> selectPage(Page reqPage, SysConfigQueryDTO req) {
        return R.success(sysConfigService.selectPage(reqPage, req));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    @SaCheckPermission("system:sysConfig:add")
    @AutoLog(module = "参数管理", title = "新增")
    public R<Void> add(@Validated @RequestBody SysConfigAddDTO req) {
        sysConfigService.add(req);
        return R.success();
    }

    /**
     * 修改
     */
    @PostMapping("/edit")
    @SaCheckPermission("system:sysConfig:edit")
    @AutoLog(module = "参数管理", title = "修改")
    public R<Void> edit(@Validated @RequestBody SysConfigEditDTO req) {
        sysConfigService.edit(req);
        return R.success();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @SaCheckPermission("system:sysConfig:remove")
    @AutoLog(module = "参数管理", title = "删除")
    public R<Void> remove(@RequestParam String ids) {
        sysConfigService.remove(ids);
        return R.success();
    }

    /**
     * 查看
     */
    @GetMapping("/view")
    @SaCheckPermission("system:sysConfig:view")
    @AutoLog(module = "参数管理", title = "查看")
    public R<SysConfigVO> view(@RequestParam String id) {
        return R.success(sysConfigService.view(id));
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    @SaCheckPermission("system:sysConfig:export")
    @AutoLog(module = "参数管理", title = "导出")
    @SneakyThrows
    public void export(SysConfigQueryDTO req, HttpServletResponse response) {
        String filename = "参数列表" + "_" + System.currentTimeMillis() + ".xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
        EasyExcel.write(response.getOutputStream())
                .head(SysConfigListVO.class)
                .sheet("参数列表")
                .doWrite(sysConfigService.selectList(req));
    }

    /**
     * 根据键值，获取value
     */
    @GetMapping("/getConfigValue")
    @AutoLog(module = "参数管理", title = "根据键值，获取value")
    public R<String> getConfigValue(@RequestParam String key) {
        return R.success(sysConfigService.getConfigValueByKey(key));
    }

    /**
     * 刷新缓存
     */
    @PostMapping("/refresh")
    @SaCheckPermission("system:sysConfig:refresh")
    @AutoLog(module = "参数管理", title = "刷新缓存")
    public R<Void> refresh() {
        sysConfigService.clearAllCache();
        return R.success();
    }

}

